package com.sso.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.sso.entity.DicHuman;
import com.sso.service.IDicHumanService;
@ManagedBean(name = "sso_listHumanBean", scope = ManagedBeanScope.REQUEST)
public class ListHumanBean {
	private final static Log logger = LogFactory
	.getLog(ListSystemBean.class);
	private IDicHumanService dicHumanService = SpringBeanHolder.
			getBean("dicHumanService");
	@Inject
	private FacesContext context;
	
	@Bind(id = "humanName", attribute = "value")
	@SaveState
	private String humanName_value;
	
	@Bind(id = "humanGrid", attribute = "binding")
	private UIDataGrid humanGrid_binding;
	
	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			humanName_value = null;
		}
	}
	
	@Action(id = "search")
	public void search() {
		humanGrid_binding.reload();
	}
	@Action(id = "reset")
	public void reset() {
		humanName_value = null;
		humanGrid_binding.reload();
	}
	
	@DataModel(id = "humanGrid")
	private List<DicHuman> getSystemGrid() {
		// 搜索条件写入搜索
		List<DicHuman> chList = new ArrayList<DicHuman>();
		if (humanName_value != null && !"".equals(humanName_value)) {
			chList = dicHumanService.getByHumanName(humanName_value);
		} else {
			chList = dicHumanService.getAllHuman();
		}
		return chList;
	}
	
	@Action(id = "deleteHuman")
	public void deleteSystem() {
		Object[] obj = humanGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		DicHuman human = (DicHuman) obj[0];
		//逻辑删除
		dicHumanService.deleteDicHuman(human);
		humanGrid_binding.reload();
	}
	
	@Action(id = "eidtSystem")
	public void eidtSystem() {
		Object[] obj = humanGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		DicHuman human = (DicHuman) obj[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/sso/editHuman.faces?id=" + human.getId();
		String script = "addCenterLayoutTab('eidtHumanTab','编辑系统用户','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
	}
}
